package com.hyy.blogtest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.hyy.blogtest.common.exception.BlogException;
import com.hyy.blogtest.common.pojo.dadaobject.BlogInfo;
import com.hyy.blogtest.common.pojo.dadaobject.UserInfo;
import com.hyy.blogtest.common.pojo.request.AddBlogRequest;
import com.hyy.blogtest.common.pojo.request.UpdateBlogRequest;
import com.hyy.blogtest.common.pojo.response.BlogInfoResponse;
import com.hyy.blogtest.common.utils.BeanUtilsConver;
import com.hyy.blogtest.mapper.BlogInfoMapper;
import com.hyy.blogtest.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        List<BlogInfo> list = null;
        try {
            //获取所有没有删除的博客，然后根据id降序排序
            list = blogInfoMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                    .eq(BlogInfo::getDeleteFlag, 0)
                    .orderByDesc(BlogInfo::getId));
        } catch (Exception e) {
            log.error("数据库获取博客列表失败：{}", e.getMessage());
            throw new BlogException(e.getMessage());
        }

        if (list == null)//数据库没有数据，返回空
            return null;
        //把对象统一转换成BlogInfoResponse
        List<BlogInfoResponse> responseList = list.stream().map(blogInfo ->
        {

            return BeanUtilsConver.converToBlog(blogInfo);
        }).collect(Collectors.toList());


        return responseList;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = blogInfoMapper.selectOne(
                new LambdaQueryWrapper<BlogInfo>()
                        .eq(BlogInfo::getDeleteFlag, 0)
                        .eq(BlogInfo::getId, blogId)
        );
        return BeanUtilsConver.converToBlog(blogInfo);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        //在数据库建表的时候已经默认设置deleteflag为0
        //创建和更新时间为插入时间，所以不用管BlogInfo中的其他字段

        BlogInfo blogInfo=BeanUtilsConver.conver(addBlogRequest);
        int result=0;
        try{
            result=blogInfoMapper.insert(blogInfo);
        }catch (Exception e){
            log.error("数据库增添博客失败");
            throw new BlogException(e.getMessage());
        }
        return result==1;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        int result=0;
        try{
            result=blogInfoMapper.deleteById(blogId);
        }catch (Exception e){
            log.error("删除博客失败，e{}",e.getMessage());
            throw new BlogException(e.getMessage());
        }

        return result==1;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {

        //根据id查看是否有这篇博客，没有返回false打印日志
        BlogInfo blogInfo=BeanUtilsConver.conver(updateBlogRequest);

        int result=blogInfoMapper.updateById(blogInfo);
        if(result==0){
            log.error("前端输入的博客id不存在");
        }
        return result==1;
    }

    ;
}
